<?php
class SalesController extends AppController {

	var $name = 'Sales';

	function index() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->Sale->recursive = 0;
			$this->set('sales', $this->paginate());
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	

	function add() {
		if(empty($this->data)){
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->Sale->create();
			if ($this->Sale->save($this->data)) {
				$this->Session->setFlash(__('The sale has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The sale could not be saved. Please, try again.', true));
			}
		}
		$products = $this->Sale->Product->find('list');
		$users = $this->Sale->User->find('list');
		$this->set(compact('products', 'users'));
	}

	

	function delete($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid id for sale', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->Sale->delete($id)) {
				$this->Session->setFlash(__('Sale deleted', true));
				$this->redirect(array('action'=>'index'));
			}
			$this->Session->setFlash(__('Sale was not deleted', true));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function payment(){
		$cart = $this->Session->read('cart');
		$user = $this->Session->read('Auth.User');
		
		if(sizeof($cart['products'])>0){
			if($user){
				$this->loadModel('Product');
				foreach ($cart['products'] as $product){
					$data['Sale']['product_id'] = $product['Product']['id'];
					$data['Sale']['user_id'] = $user['id'];
					for($i = 0; $i<$product['Product']['qty']; $i++){
						$this->Product->id = $product['Product']['id'];
						$this->Product->inc('sold_count');
						$this->Sale->create();
						$this->Sale->save($data);
					}			
				}
				$this->Session->write('cart',array('products'=>array(),'sum'=>0));
				$this->Session->setFlash(__('Siparişiniz tamamlanmıştır.', true));
				$this->redirect(array('controller'=>'pages','action' => 'index'));
			} else {
				$this->Session->setFlash(__('Siparişiniz tamamlanamadı. Sipariş yapmanız için giriş yapmanız gereklidir!', true));
				$this->redirect(array('controller'=>'users','action' => 'login'));
			}
		} else {
			$this->Session->setFlash(__('Siparişiniz tamamlanamadı.Sepetiniz boş!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function beforeFilter() {
	    parent::beforeFilter();
	    $this->Auth->allow(array('*'));
    }
}
